/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sadhar.sipp.patokanharga;

import java.util.List;
import java.util.Map;

import org.sadhar.errhandler.ClassAntiNull;
import org.sadhar.sia.common.ClassConnection;
import org.sadhar.sia.common.ClassUtility;

/**
 * 
 * @author Yohan Hardinugroho
 */
public class PatokanHargaDAOImpl implements PatokanHargaDAO {

    @Override
    public void deleteByTahun(String tahun) {
	String sql = "Delete from ppmk.patokanHarga where tahun=?";
	ClassConnection.getJdbc().update(
		sql,
		new Object[] { ClassAntiNull.AntiNullString(ClassUtility
			.quote(tahun)) });
	String sql2 = "Delete from ppmk.nilaiPatokanHarga where tahun=?";
	ClassConnection.getJdbc().update(
		sql2,
		new Object[] { ClassAntiNull.AntiNullString(ClassUtility
			.quote(tahun)) });

    }

    @Override
    public List<Map> select() {
	String sql = "SELECT * FROM ppmk.patokanHarga p";
	return ClassConnection.getJdbc().queryForList(sql);
    }

    @Override
    public void insert(PatokanHarga patokanHarga) {
	String sql = "insert into ppmk.patokanHarga (tahun,tglBatasMaksimumPembelian) values (?,?)";
	ClassConnection.getJdbc().update(
		sql,
		new Object[] { ClassUtility.quote(patokanHarga.getTahun()),
			patokanHarga.getTglBatasMaximumPembelian() });
	// TODO Auto-generated method stub

    }

    @Override
    public void update(PatokanHarga patokanHarga) {
	String sql = "update ppmk.patokanHarga set tglBatasMaksimumPembelian=? where tahun=? ";
	ClassConnection.getJdbc().update(
		sql,
		new Object[] { patokanHarga.getTglBatasMaximumPembelian(),
			ClassUtility.quote(patokanHarga.getTahun()) });

    }

    @Override
    public PatokanHarga load(String tahun) {
	String sql = "Select tahun,tglBatasMaksimumPembelian from ppmk.patokanHarga where tahun=?";
	List<Map> result = ClassConnection.getJdbc().queryForList(sql,
		new Object[] { ClassUtility.quote(tahun) });
	PatokanHarga ph = null;
	for (Map m : result) {
	    ph = new PatokanHarga();
	    ph.setTahun(tahun);
	    ph.setTglBatasMaximumPembelian(ClassAntiNull.AntiNullDate(m
		    .get("tglBatasMaksimumPembelian")));
	}
	// TODO Auto-generated method stub
	return ph;
    }

    @Override
    public boolean isExist(String tahun) {
	String sql = "Select tahun,tglBatasMaksimumPembelian from ppmk.patokanHarga where tahun=?";
	List<Map> result = ClassConnection.getJdbc().queryForList(sql,
		new Object[] { ClassUtility.quote(tahun) });

	return (result.size() > 0);

    }

}
